<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="政策名称" prop="policyTitle">
        <el-input
          v-model="queryParams.policyTitle"
          placeholder="请输入政策名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政策状态" prop="policyStatus">
        <el-select v-model="queryParams.policyStatus" placeholder="请选择政策状态" clearable size="small">
          <el-option label="有效" value="有效" />
          <el-option label="无效" value="无效" />
        </el-select>
      </el-form-item>
      
      <!-- <el-form-item label="政策级别" prop="policyLevel">
        <el-input
          v-model="queryParams.policyLevel"
          placeholder="请输入政策级别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="政策类型" prop="policyType">
        <el-select v-model="queryParams.policyType" placeholder="请选择政策类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="政策来源" prop="policySource">
        <el-input
          v-model="queryParams.policySource"
          placeholder="请输入政策来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="政策索引号" prop="policyIndexnum">
        <el-input
          v-model="queryParams.policyIndexnum"
          placeholder="请输入政策索引号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政策文号" prop="policyDocnum">
        <el-input
          v-model="queryParams.policyDocnum"
          placeholder="请输入政策文号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政策发文时间" prop="policyFwtime">
        <el-date-picker clearable size="small"
          v-model="queryParams.policyFwtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择政策发文时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否撤销(1为否,0为是)" prop="ifCancel">
        <el-input
          v-model="queryParams.ifCancel"
          placeholder="请输入是否撤销(1为否,0为是)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="撤销时间" prop="cancelTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.cancelTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择撤销时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后修改时间" prop="modifiedTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.modifiedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择最后修改时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:policys:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:policys:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:policys:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:policys:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="policysList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="政策ID" align="left" prop="policyId" />
      <el-table-column label="政策名称" align="left" prop="policyTitle"  width="300" show-overflow-tooltip />
      <el-table-column label="政策级别" align="left" prop="policyLevel"  width="200"  />
      <el-table-column label="政策状态" align="left" prop="policyStatus"   width="200" />
      <el-table-column label="政策类型" align="left" prop="policyType"  width="200" />
      <el-table-column label="政策来源" align="left" prop="policySource"  width="200"   />
      <el-table-column label="政策内容" align="left" prop="policyContent"  width="300" show-overflow-tooltip />
      <el-table-column label="政策发布时间" align="left" prop="policyFbtime"  width="200" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.policyFbtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政策索引号" align="left" prop="policyIndexnum"   width="200" />
      <el-table-column label="政策文号" align="left" prop="policyDocnum"  width="200"  />
      <el-table-column label="政策发文时间" align="left" prop="policyFwtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.policyFwtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否撤销(1为否,0为是)" align="left" prop="ifCancel"  width="200"/>
      <el-table-column label="撤销时间" align="left" prop="cancelTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="left" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width"  width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:policys:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:policys:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改政策信息对话框 -->
    <el-dialog  :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form  label-width="100px" ref="form" :model="form" :rules="rules" >
        <el-form-item label="政策名称" prop="policyTitle">
          <el-input v-model="form.policyTitle" placeholder="请输入政策名称" />
        </el-form-item>
        <el-form-item label="政策级别" prop="policyLevel">
          <el-input v-model="form.policyLevel" placeholder="请输入政策级别" />
        </el-form-item>
        <el-form-item label="政策状态">
          <el-radio-group v-model="form.policyStatus">
            <el-radio label="有效">有效</el-radio>
            <el-radio label="无效">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="政策类型" prop="policyType">
          <el-select v-model="form.policyType" placeholder="请选择政策类型">
            <el-option label="信息产业(含电信)" value="信息产业(含电信)" />
          </el-select>
        </el-form-item> -->

        <el-form-item label="政策类型" prop="policyType">
          <el-input v-model="form.policyType" placeholder="政策类型" />
        </el-form-item>

        <el-form-item label="政策来源" prop="policySource">
          <el-input v-model="form.policySource" placeholder="请输入政策来源" />
        </el-form-item>
        <el-form-item label="政策内容">
          <editor v-model="form.policyContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="政策发布时间" prop="policyFbtime">
          <el-date-picker clearable size="small"
            v-model="form.policyFbtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择政策发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="政策索引号" prop="policyIndexnum">
          <el-input v-model="form.policyIndexnum" placeholder="请输入政策索引号" />
        </el-form-item>
        <el-form-item label="政策文号" prop="policyDocnum">
          <el-input v-model="form.policyDocnum" placeholder="请输入政策文号" />
        </el-form-item>
        <el-form-item label="政策发文时间" prop="policyFwtime">
          <el-date-picker clearable size="small"
            v-model="form.policyFwtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择政策发文时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否撤销" prop="ifCancel">
          <el-input v-model="form.ifCancel" placeholder="请输入是否撤销(1为否,0为是)" />
        </el-form-item>
        <el-form-item label="撤销时间" prop="cancelTime">
          <el-date-picker clearable size="small"
            v-model="form.cancelTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择撤销时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后修改时间" prop="modifiedTime">
          <el-date-picker clearable size="small"
            v-model="form.modifiedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPolicys, getPolicys, delPolicys, addPolicys, updatePolicys, exportPolicys } from "@/api/zsj/policys";
import Editor from '@/components/Editor';

export default {
  name: "Policys",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 政策信息表格数据
      policysList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        policyTitle: null,
        policyLevel: null,
        policyStatus: null,
        policyType: null,
        policySource: null,
        policyContent: null,
        policyFbtime: null,
        policyIndexnum: null,
        policyDocnum: null,
        policyFwtime: null,
        ifCancel: null,
        cancelTime: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询政策信息列表 */
    getList() {
      this.loading = true;
      listPolicys(this.queryParams).then(response => {
        this.policysList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        policyId: null,
        policyTitle: null,
        policyLevel: null,
        policyStatus: "0",
        policyType: null,
        policySource: null,
        policyContent: null,
        policyFbtime: null,
        policyIndexnum: null,
        policyDocnum: null,
        policyFwtime: null,
        ifCancel: null,
        cancelTime: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.policyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加政策信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const policyId = row.policyId || this.ids
      getPolicys(policyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改政策信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.policyId != null) {
            updatePolicys(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPolicys(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const policyIds = row.policyId || this.ids;
      this.$confirm('是否确认删除政策信息编号为"' + policyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPolicys(policyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有政策信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPolicys(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
